<?php

class ArticulosController extends AppController {

    var $name = 'Articulos';
    var $helpers = array('Ajax', 'Javascript', 'Time');

    function menuCategoria($categoria = null) {
        if ( $categoria) {
            // TODO la busqueda debe ser por orden
            return $this->Articulo->find('all', array(
                'conditions' => array('Articulo.categoria' => $categoria), //array of conditions
                'fields' => array('Articulo.titulo', 'Articulo.id'), //array of field names
            ));
        }
    }

    function view($id = null) {
        $articulo = $this->Articulo->read(null, $id);
        if (!$id || !$articulo) {
            $this->Session->setFlash(__('Artículo inválido', true));
            $this->redirect(array('controller' => 'pages', 'action' => 'display', 'home'));
        } else {
            $this->set('articulo', $this->Articulo->read(null, $id));
        }
    }

    function view_profesional($id = null) {
        $articulo = $this->Articulo->read(null, $id);
        if (!$id || !$articulo) {
            // TODO la busqueda debe ser por orden
            $this->set('articulo', $this->Articulo->findByCategoria('PROFESIONALES'));
        } else {
            $this->set('articulo', $this->Articulo->read(null, $id));
        }
    }

    function view_eventos() {

       // TODO la busqueda debe ser por orden
       $this->set('articulos', $this->menuCategoria('EVENTOS'));

    }
    
    function admin_index($categoria = null) {
        if (!$categoria) {
//            $this->Session->setFlash(__('Falta el parametro de Categoria.', true));
//            $this->redirect(array('controller' => 'pages', 'action' => 'index'));


            $this->redirect(array('controller' => 'articulos', 'action' => 'indexarti'));
        }

        $this->paginate = array(
            'conditions' => array('Articulo.categoria' => $categoria),
        );

        $this->set('categoria', $categoria);
        $this->set('articulos', $this->paginate());
    }

    function admin_indexarti() {

//            $this->Session->setFlash(__('Falta el parametro de Categoria.', true));
//            $this->redirect(array('controller' => 'pages', 'action' => 'index'));

        $this->set('esArticulo', true);
    }

    function admin_view($id = null, $categoria=null) {
        if (!$id) {
            $this->Session->setFlash(__('Invalid articulo', true));
            $this->redirect(array('action' => 'index'));
        }

        if (!$categoria) {
            $this->Session->setFlash(__('Falta el parametro de Categoria.', true));
            $this->redirect(array('controller' => 'pages', 'action' => 'index'));
        }
        $this->set('categoria', $categoria);
        $this->set('articulo', $this->Articulo->read(null, $id));
    }

    function admin_add($categoria = null) {
        if (!empty($this->data)) {
            $this->Articulo->create();

            $newName = $this->Articulo->saveImage($this->params['data']['Articulo']['imagen']);

            if (isset($newName)) {
                $this->params['data']['Articulo']['imagen'] = $newName;
            } else {
                $this->params['data']['Articulo']['imagen'] = null;
            }

            if ($this->Articulo->save($this->data)) {
                $this->Session->setFlash(__('The articulo has been saved', true));

                $this->redirect(array('action' => 'index', $this->data['Articulo']['categoria']));
            } else {
                $this->Session->setFlash(__('The articulo could not be saved. Please, try again.', true));
            }
        }
//        if (!$categoria) {
//            $this->Session->setFlash(__('Falta el parametro de Categoria.', true));
//            $this->redirect(array('controller' => 'pages', 'action' => 'index'));
//        }

        $this->set('categoria', $categoria);
    }

    function admin_edit($id = null, $categoria = null) {
        if (!$id && empty($this->data)) {
            $this->Session->setFlash(__('Invalid articulo', true));
            $this->redirect(array('action' => 'index'));
        }
        if (!empty($this->data)) {

            $newName = $this->Articulo->saveImage($this->params['data']['Articulo']['imagen']);

            if (isset($newName)) {
                $this->params['data']['Articulo']['imagen'] = $newName;
            } else {
                $this->params['data']['Articulo']['imagen'] = null;
            }

            if ($this->Articulo->save($this->data)) {
                $this->Session->setFlash(__('The articulo has been saved', true));
                $this->redirect(array('action' => 'index', $this->data['Articulo']['categoria']));
            } else {
                $this->Session->setFlash(__('The articulo could not be saved. Please, try again.', true));
            }
        }
        if (empty($this->data)) {
            $this->data = $this->Articulo->read(null, $id);
        }
        
        $this->set('categoria', $categoria);
        $this->set('id', $id);
    }

    function admin_delete($id = null) {
        $articleData = $this->Articulo->read();
        if (!$id) {
            $this->Session->setFlash(__('Invalid id for articulo', true));
            $this->redirect(array('action' => 'index'));
        }
        if ($this->Articulo->delete($id)) {
            $this->Articulo->delImage($articleData['Articulo']['imagen']);
            $this->Session->setFlash(__('Articulo deleted', true));
            $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash(__('Articulo was not deleted', true));
        $this->redirect(array('action' => 'index'));
    }

}

?>